package routers

import (
	"fox/jsonrpc"
	"fox/websockets"
	"github.com/gin-gonic/gin"
	"net/http"
)

func InitRouters(r *gin.Engine) {
	var (
		adminUser       *gin.RouterGroup
		adminRole       *gin.RouterGroup
		adminPermission *gin.RouterGroup
		adminSys        *gin.RouterGroup
	)

	r.Any("/json-rpc", func(c *gin.Context) {
		var (
			method string
			ok     bool
		)
		method = c.GetHeader("method")
		if method == "" {
			c.JSON(http.StatusOK, gin.H{
				"message": "缺少方法",
			})
			return
		}
		if _, ok = jsonrpc.GJsonRpc.JsonRpcFunMap[method]; ok {
			jsonrpc.GJsonRpc.JsonRpcFunMap[method](c)
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"message": "方法不存在",
		})
		return
	})

	//websocket
	r.GET("/websocket", websockets.Doing)

	r.GET("/ping", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message:news": "pong",
		})
		return
	})

	//获取全部路由
	r.GET("getRouters", func(c *gin.Context) {
		var (
			routers gin.RoutesInfo
		)
		type Router struct {
			Method  string
			Path    string
			Handler string
		}

		var router Router
		var routerMap []Router

		routers = r.Routes()
		for _, routerPath := range routers {
			router.Method = routerPath.Method
			router.Path = routerPath.Path
			//router.Handler  = routerPath.Handler
			routerMap = append(routerMap, router)
		}
		c.JSON(http.StatusOK, gin.H{
			"routers": routerMap,
		})
	})

	//系统操作
	adminSys = r.Group("/admin/sys")
	{
		AdminSysRouter(adminSys)
	}

	//user路由组
	adminUser = r.Group("/admin/user")
	{
		AdminUserRouter(adminUser)
	}
	//role路由组
	adminRole = r.Group("/admin/role")
	{
		AdminRoleRouter(adminRole)
	}
	//permission
	adminPermission = r.Group("/admin/permission")
	{
		AdminPermissionRouter(adminPermission)
	}

}
